package sg.edu.nus.iss.vmcs.customer;

import java.awt.GridLayout;
import java.awt.Panel;
import java.util.HashMap;
import java.util.Iterator;

/*
 * @author nyi nyi than
 */

public class DrinkSelectionBox extends Panel
{
    private static final long serialVersionUID = 1L;

    HashMap<String, DrinkSelectionItem> drinkItems = new HashMap<String, DrinkSelectionItem>();

    public DrinkSelectionBox(CustomerPanelPresenter presenter)
    {
	this.setLayout(new GridLayout(0, 1));

	for (DrinkSelectionItem item : presenter.getDrinkSelectionItems())
	{
	    drinkItems.put(item.getName(), item);
	    add(item);
	}
    }

    public void setStatus(boolean status, String currentItemName)
    {
	Iterator<DrinkSelectionItem> iterator = drinkItems.values().iterator();
	while (iterator.hasNext())
	{
	    DrinkSelectionItem temp = iterator.next();
	    String tempName = temp.getName();
	    if (!tempName.equalsIgnoreCase(currentItemName))
		temp.setItemState(status);
	}
    }

    public void reset()
    {
	setStatus(true, "nil");
    }

    public int getSelectedItemPrice(String currentItemName)
    {
	int price = 0;
	Iterator<DrinkSelectionItem> iterator = drinkItems.values().iterator();
	while (iterator.hasNext())
	{
	    DrinkSelectionItem temp = iterator.next();
	    String tempName = temp.getName();
	    if (tempName.equalsIgnoreCase(currentItemName))
	    {
		price = temp.getPrice();
		break;
	    }
	}
	
	return price;
    }
    
    public void dispense(String itemName){
	Iterator<DrinkSelectionItem> iterator = drinkItems.values().iterator();
	while (iterator.hasNext())
	{
	    DrinkSelectionItem temp = iterator.next();
	    String tempName = temp.getName();
	    if (tempName.equalsIgnoreCase(itemName))
	    {
		temp.reduce();
		break;
	    }
	}
    }
}
